#
# This file is part of the kernelstudio package.
#
# (c) 2014-2025 zlin <admin@kernelstudio.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.
import typing as t
from abc import ABC, abstractmethod
from flask import Flask

from ksai.json import ApplicationJSONProvider


class ApplicationException(Exception):
    message: t.Optional[str] = "Error."
    code: t.Optional[str] = "500"
    status: t.Optional[int] = 500,

    def __init__(
            self,
            message: t.Optional[str] = "Error.",
            code: t.Optional[str] = "500",
            status: t.Optional[int] = 500):
        self.message = message
        self.code = code
        self.status = status


class Application(Flask):
    json_provider_class = ApplicationJSONProvider
    pass


class ApplicationInitializer(ABC):

    def __init__(self):
        pass

    @abstractmethod
    def initialize(self, app: Application) -> t.Any:
        raise NotImplementedError


class CompositeInitializer(ApplicationInitializer):

    def __init__(self, initializers=list[ApplicationInitializer]):
        super().__init__()
        self.initializers = initializers or []

    def initialize(self, app: Application) -> t.Any:
        for initializer in self.initializers:
            initializer.initialize(app)